package datastructure.stack;

import java.util.Arrays;

/**
 * @author XY
 * @version 1.0
 * @date 2022/2/25 9:47
 * @Description
 */
public class Leet496 {
    public static int[] nextGreaterElement(int[] nums1, int[] nums2) {
        int i=0;
        int[] res = new int[nums1.length];
        while(i<nums1.length){
            int num = nums1[i];
            for (int j = 0; j < nums2.length; j++) {
                if(nums2[j]==num){
                    boolean flag = false;
                    for (int n = j+1; n < nums2.length; n++) {
                        if(nums2[n]>nums1[i]){
                            res[i] = nums2[n];
                            flag = true;
                            break;
                        }
                    }
                    if(!flag){
                        res[i] = -1;
                    }
                    i++;
                }
            }
        }

        return Arrays.copyOfRange(res,0,i);
    }

    public static int[] nextGreaterElement1(int[] nums1, int[] nums2) {
        int[] res= new int[nums1.length];
        for (int i = 0; i < nums1.length; i++) {
            int j = 0;
            //没找到就一直找啊，找到了就不动了
            while(j<nums2.length&&nums2[j]!=nums1[i]){
                j++;
            }
            int k = j+1;
            while(k<nums2.length&&nums2[k]<nums1[i]){
                k++;
            }
            res[i] = k<nums2.length?nums2[k]:-1;
        }
        return res;
    }

    public static void main(String[] args) {
        int[] arr1 = {2,4};
        int[] arr2 = {1,2,3,4};
        for (int i : nextGreaterElement(arr1, arr2)) {
            System.out.println(i);
        }
    }
}
